package net.flyinggroup.si;

import net.flyinggroup.si.integration.ICargoGateway;
import net.flyinggroup.si.model.Cargo;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.integration.support.MessageBuilder;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Walker Zhang
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        ICargoGateway orderGateway = context.getBean(ICargoGateway.class);

        getCargoBatchMap()
                .forEach((batchId, cargoList) -> orderGateway.processCargoRequest(
                        MessageBuilder.withPayload(cargoList)
                                .setHeaderIfAbsent("CARGO_BATCH_ID", batchId)
                                .build()));
    }

    public static Map<Integer, List<Cargo>> getCargoBatchMap() {
        return Map.of(1, Arrays.asList(
                Cargo.builder().trackingId(1).receiverName("Receiver_Name1").deliveryAddress("Address1")
                        .weight(0.5).shippingType(Cargo.ShippingType.DOMESTIC).region(1)
                        .description("Radio").build(),
                Cargo.builder().trackingId(2).receiverName("Receiver_Name2").deliveryAddress("Address2")
                        .weight(2000).shippingType(Cargo.ShippingType.INTERNATIONAL).deliveryDayCommitment(3)
                        .description("Furniture").build(),
                Cargo.builder().trackingId(3).receiverName("Receiver_Name3").deliveryAddress("Address3")
                        .weight(5).shippingType(Cargo.ShippingType.INTERNATIONAL).deliveryDayCommitment(2)
                        .description("TV").build(),
                Cargo.builder().trackingId(4).receiverName("Receiver_Name4").deliveryAddress("Address4")
                        .weight(8).deliveryDayCommitment(2).description("Chair").build()), 2, Arrays.asList(
                Cargo.builder().trackingId(5).receiverName("Receiver_Name5").deliveryAddress("Address5")
                        .weight(1200).shippingType(Cargo.ShippingType.DOMESTIC).region(2)
                        .description("Refrigerator").build(),
                Cargo.builder().trackingId(6).receiverName("Receiver_Name6").deliveryAddress("Address6")
                        .weight(20).shippingType(Cargo.ShippingType.DOMESTIC).region(3)
                        .description("Table").build(),
                Cargo.builder().trackingId(7).receiverName("Receiver_Name7").deliveryAddress("Address7")
                        .weight(5).deliveryDayCommitment(1)
                        .description("TV").build(),
                Cargo.builder().trackingId(8).receiverName("Receiver_Name8").deliveryAddress("Address8")
                        .weight(200).shippingType(Cargo.ShippingType.DOMESTIC).region(2)
                        .description("Washing Machine").build(),
                Cargo.builder().trackingId(9).receiverName("Receiver_Name9").deliveryAddress("Address9")
                        .weight(4.75).shippingType(Cargo.ShippingType.INTERNATIONAL).deliveryDayCommitment(1)
                        .description("Document").build()));
    }
}